package com.zaizai.service.impl;

import java.util.Date;
import java.util.List;

import com.zaizai.domain.Commodity;
import com.zaizai.domain.CommodityPrice;
import com.zaizai.mapper.CommodityMapper;
import com.zaizai.service.ICommodityPriceService;
import com.zaizai.service.ICommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 商品详情Service业务层处理
 *
 * @author zaiZai
 * @date 2024-10-21
 */
@Service
public class CommodityServiceImpl implements ICommodityService {
    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private ICommodityPriceService commodityPriceService;

    /**
     * 查询商品详情
     *
     * @param id 商品详情主键
     * @return 商品详情
     */
    @Override
    public Commodity selectCommodityById(Long id) {
        return commodityMapper.selectCommodityById(id);
    }

    /**
     * 查询商品详情列表
     *
     * @param commodity 商品详情
     * @return 商品详情
     */
    @Override
    public List<Commodity> selectCommodityList(Commodity commodity) {
        return commodityMapper.selectCommodityList(commodity);
    }

    /**
     * 新增商品详情
     *
     * @param commodity 商品详情
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCommodity(Commodity commodity) {
        //创建时间
        commodity.setCreateTime(new Date());
        //修改价格，转为为分
        commodity.setPrice(commodity.getPrice() * 100);
        //回显商品id
        int result = commodityMapper.insertCommodity(commodity);
        //新增的时候把当时的单价记录到单价表中
        CommodityPrice commodityPrice = CommodityPrice.builder()
                .commodityId(commodity.getId())
                .price(commodity.getPrice())
                .time(commodity.getCreateTime())
                .build();
        commodityPriceService.insertCommodityPrice(commodityPrice);
        return result;
    }

    /**
     * 修改商品详情
     *
     * @param commodity 商品详情
     * @return 结果
     */
    @Override
    @Transactional
    public int updateCommodity(Commodity commodity) {
        //修改时间
        commodity.setUpdateTime(new Date());
        //修改价格，转为为分
        commodity.setPrice(commodity.getPrice() * 100);
        //修改的时候把当时的单价记录到单价表中
        commodityPriceService.insertCommodityPrice(CommodityPrice.builder()
                .commodityId(commodity.getId())
                .price(commodity.getPrice())
                .time(commodity.getUpdateTime())
                .build());
        return commodityMapper.updateCommodity(commodity);
    }

    /**
     * 批量删除商品详情
     *
     * @param ids 需要删除的商品详情主键
     * @return 结果
     */
    @Override
    public int deleteCommodityByIds(Long[] ids) {
        return commodityMapper.deleteCommodityByIds(ids);
    }

    /**
     * 删除商品详情信息
     *
     * @param id 商品详情主键
     * @return 结果
     */
    @Override
    public int deleteCommodityById(Long id) {
        return commodityMapper.deleteCommodityById(id);
    }
}
